package com.qinghuan.threads.chapterTwo;

import sun.misc.Unsafe;

import java.lang.reflect.Field;

/**
 * @Author: dubao
 * @Date: 2022/5/21 09:59
 * @Description:
 */
public class Test2_9_2_update {
}

class TestUnSafeNew {
    static Unsafe unsafe;
    static long stateOffset;
    private volatile long state = 0;
    static {
        try {
            Field field = Unsafe.class.getDeclaredField("theUnsafe");
            field.setAccessible(true);
            unsafe = (Unsafe) field.get(null);
            stateOffset = unsafe.objectFieldOffset(TestUnSafeNew.class.getDeclaredField("state"));
        } catch (Exception e) {
            System.out.println(e.getLocalizedMessage());
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        TestUnSafeNew test = new TestUnSafeNew();
        boolean success = unsafe.compareAndSwapInt(test, stateOffset, 0, 1);
        System.out.println(success);
    }
}
